const db = require("../../../db");
const {SYSTEM_ERROR} = require("../../../utils/errorCode");
exports.main = (req, res) => {

    const {userId} = req.auth;
    // 获取购物车信息
    const sql = `select * from address where userId = ? and isDelete = 0 and isDefault = 1`;
    db.query(sql, userId, (err, result) => {
        if (err) {
            console.log(err);
            return res.err(SYSTEM_ERROR);
        }
        // 如果没有默认查询最近创建的地址
        console.log(result)
        if (!result[0]) {

            const sql = `select * from address where isDelete = 0 and userId = ? and updateTime = (select max(updateTime) 
                    from address where isDelete = 0 and userId = ? group by userId)`;
            db.query(sql, [userId,userId], (err, result) => {
                if (err) {
                    console.log(err);
                    return res.err(SYSTEM_ERROR);
                }
                return res.sd(result[0]);
            })
        } else {
            return res.sd(result[0]);
        }

    })
}

